<?php 
/*
 module:		提现审核管理
 create_time:	2022-04-08 18:24:09
 author:		
 contact:		
*/

namespace app\backend\controller;

use app\api\service\ZhBalanceService;
use app\backend\service\ZhWithdrawalExamineService;
use app\backend\model\ZhWithdrawalExamine as ZhWithdrawalExamineModel;
use think\facade\Db;
use app\backend\model\Member as MemberModel;
use app\api\model\ZhBalanceLog as ZhBalanceLogModel;
use app\api\service\PaymentService;

class ZhWithdrawalExamine extends Admin {


	/*首页数据列表*/
	function index(){
		if (!$this->request->isAjax()){
			return view('index');
		}else{
			$limit  = $this->request->post('limit', 20, 'intval');
			$offset = $this->request->post('offset', 0, 'intval');
			$page   = floor($offset / $limit) +1 ;

			$where = [];
			$where['order_sn'] = $this->request->param('order_sn', '', 'serach_in');
			$where['status'] = $this->request->param('status', '', 'serach_in');
			$where['pay_type'] = $this->request->param('pay_type', '', 'serach_in');
            $where['wxapp_id'] = session('backend.wxapp_id');
			$order  = $this->request->post('order', '', 'serach_in');	//排序字段 bootstrap-table 传入
			$sort  = $this->request->post('sort', '', 'serach_in');		//排序方式 desc 或 asc

			$field = 'id,nickname,order_sn,price,status,createtime,withdrawal_no';
			$orderby = ($sort && $order) ? $sort.' '.$order : 'id desc';

			$res = ZhWithdrawalExamineService::indexList(formatWhere($where),$field,$orderby,$limit,$page);
			return json($res);
		}
	}

	/*修改*/
	function update(){
		if (!$this->request->isPost()){
			$id = $this->request->get('id','','serach_in');
			if(!$id) $this->error('参数错误');
			$this->view->assign('info',checkData(ZhWithdrawalExamineModel::find($id)));
			return view('update');
		}else{
			$postField = 'id,nickname,order_sn,price,status,createtime,withdrawal_no';
			$data = $this->request->only(explode(',',$postField),'post',null);
            $findData = checkData(ZhWithdrawalExamineModel::find($data['id']));
            $memberData = MemberModel::where([
                                            'wxapp_id'=> $findData['wxapp_id'],
                                            'uid'     => $findData['u_id']
                                        ])->find();

			if($data['status'] == 2 && $memberData){
                /*
                $newBalance = round($memberData['balance'] + $findData['price'],2);
                MemberModel::where([
                                'wxapp_id'=> $findData['wxapp_id'],
                                'uid'     => $findData['u_id']
                            ])->update(['balance'=>$newBalance]);
                ZhBalanceLogModel::insert([
                                    'u_id'         => $findData['u_id'],
                                    'wxapp_id'     => $findData['wxapp_id'],
                                    'start_balance'=> $memberData['balance'],
                                    'end_balance'  => $newBalance,
                                    'type'         => '收入',
                                    'remake'       => '提现被拒绝',
                                    'create_time'  => time(),
                                    'num'          => $findData['price'],
                                    'order_sn'     => $findData['order_sn']
                ]);*/
			    ZhBalanceService::updateMoneyBalanceLog($findData['wxapp_id'], $findData['u_id'], $findData['price'], "收入", '提现被拒绝', "");
            }elseif ($data['status'] == 1 && $memberData){

                /*
                try{
                    $js_pay = PaymentService::instance($findData['wxapp_id'])->tixain($findData['order_sn'], $memberData['openid'], $findData['price'], '余额提现');
    
                    ZhBalanceLogModel::where([
                        'wxapp_id'=> $findData['wxapp_id'],
                        'u_id'    => $findData['u_id'],
                        'order_sn'=> $findData['order_sn']
                    ])->update(['wx_order_sn'=>$js_pay['payment_no']]);
                }catch (\Exception $e){
                    return json(['status'=>'01','msg'=>$e->getMessage()]);
                }*/
                
            }
			$res = ZhWithdrawalExamineService::update($data);
			return json(['status'=>'00','msg'=>'修改成功']);
		}
	}

	/*查看详情*/
	function view(){
		$id = $this->request->get('id','','serach_in');
		if(!$id) $this->error('参数错误');
		$this->view->assign('info',ZhWithdrawalExamineModel::find($id));
		return view('view');
	}



}

